package com.ais.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ais.dao.TerminalDao;
import com.ais.vo.Terminal;

@Service("terminalManage")
public class TerminalManage {
	public void setTerminalDao(TerminalDao terminalDao) {
		this.terminalDao = terminalDao;
	}

	@Autowired
	private TerminalDao terminalDao;

	public void addTerminal(Terminal terminal) {
		String terminalNo = this.getTerminalNo();
		terminal.setTerminalNo(terminalNo);
		this.terminalDao.addTerminal(terminal);
	}
	
	public void updateTerminal(Terminal terminal){
		this.terminalDao.updateTerminal(terminal);
	}
	
	public void deleteTerminal(String terminalNo){
		this.terminalDao.deleteTerminal(terminalNo);
	}
	/**
	 * 获取下一个设备编号
	 * @return
	 */
	public String getTerminalNo(){
		String terminalNo = this.terminalDao.getTerminalNo();
		

		if ("".equals(terminalNo) || "null".equals(terminalNo)) {
			return this.getTodayString()+ "-001";
		} else {
			if (terminalNo.length() != 11) {
				return  this.getTodayString()+ "-001";
			} else {
				String indexStr = terminalNo.substring(terminalNo
						.length() - 3, terminalNo.length());
				int index = Integer.valueOf(indexStr) + 1;
				String nextIndex = String.format("%03d", index);
				return this.getTodayString()+ "-" + nextIndex;
			}
		}
	}
	
	/**
	 * 获取当日时间 格式：yyyyMMdd
	 * 
	 * @return
	 */
	public String getTodayString() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date date = new Date();
		String today = format.format(date);
		return today;
	}
}
